package com.nd.android.musicboxlibrary.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.common.utils.StarAppUtils;
import com.nd.android.musicboxlibrary.R;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.business.comm.NdException;
import com.nd.android.musicboxlibrary.interf.ManagerFactory;
import com.nd.android.musicboxlibrary.service.player.MusicService;
import com.nd.android.musicboxlibrary.ui.adapter.PlayListAdapter;
import com.nd.android.musicboxlibrary.ui.common.UnMixable;
import com.nd.android.musicboxlibrary.ui.common.ViewInjectorWithReflect;
import com.nd.android.musicboxlibrary.util.ConfigHelper;
import com.nd.android.musicboxlibrary.util.MusicBoxUtils;
import com.nd.android.musicboxlibrary.util.NdTask;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * <br>
 * Created 2014年7月5日 下午4:11:54
 *
 * @version
 * @author wat.ztag
 *
 * @see
 */
public class PlayListFragment extends Fragment implements OnClickListener {

    private ViewHolder holder = new ViewHolder();

    /**
     *
     * <br>
     * Created 2014年9月11日 下午11:39:36
     *
     * @version PlayListFragment
     * @author wat.ztag
     *
     * @see
     */
    private class ViewHolder implements UnMixable {
        ListView lst;
        TextView txtTitle;
        ImageView btnLeft, btnRight;

        /**
         *
         * <br>
         * Created 2014年10月7日 下午10:09:06
         *
         * @param list ListView
         * @author wat.ztag
         */
        void setLst(ListView list) {
            this.lst = list;
        }

        /**
         *
         * <br>
         * Created 2014年10月7日 下午10:09:09
         *
         * @param title TextView
         * @author wat.ztag
         */
        void setTxtTitle(TextView title) {
            this.txtTitle = title;
        }

        /**
         *
         * <br>
         * Created 2014年10月7日 下午10:09:11
         *
         * @param left ImageView
         * @author wat.ztag
         */
        void setBtnLeft(ImageView left) {
            this.btnLeft = left;
        }

        /**
         *
         * <br>
         * Created 2014年10月7日 下午10:09:14
         *
         * @param right ImageView
         * @author wat.ztag
         */
        void setBtnRight(ImageView right) {
            this.btnRight = right;
        }

    }

    private PlayListAdapter mAdapter;

    private List<MusicInfo> singles = new ArrayList<MusicInfo>();

    private int mCurrentLoopType;

    private int[] mLoopTypeResource = { R.drawable.mbx_playlist_queue_selector,
            R.drawable.mbx_playlist_single_selector, R.drawable.mbx_playlist_random_selector };

    private int[] mTitleResource = { R.string.mbx_playlist_title_queue,
            R.string.mbx_playlist_title_single, R.string.mbx_playlist_title_random };

    private MusicReceiver mReceiver;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater. inflate(R.layout.mbx_play_list_activity, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ViewInjectorWithReflect.injectView(holder, this);
        initData();
        setupView();
        addListeners();

        mReceiver = new MusicReceiver();
        getActivity().registerReceiver(mReceiver, new IntentFilter(MusicBoxUtils.getMusicServiceAction()));

        new GetMusicListOfPlaying(getActivity(), R.string.mbx_runing).executeByVersion();
    }

    @Override
    public void onDestroy() {
        if (mReceiver != null) {
            getActivity().unregisterReceiver(mReceiver);
        }
        super.onDestroy();
    }

    /**
     *
     * <br>
     * Created 2014年9月11日 下午11:39:41
     *
     * @author wat.ztag
     */
    protected void initData() {
        mCurrentLoopType = ConfigHelper.getInstance(getActivity()).loadIntKey(
                MusicService.LOOP_TYPE, 0);
        holder.btnLeft.setImageResource(mLoopTypeResource[mCurrentLoopType]);
        holder.txtTitle.setText(getString(mTitleResource[mCurrentLoopType], singles.size()));

        holder.btnRight.setVisibility(View.VISIBLE);
    }

    /**
     *
     * <br>
     * Created 2014年9月11日 下午11:39:45
     *
     * @author wat.ztag
     */
    private void setupView() {
        mAdapter = new PlayListAdapter(this.getActivity());
        holder.lst.setAdapter(mAdapter);
        mAdapter.setDatas(singles);
        StarAppUtils.setViewTypeFace(holder.txtTitle, false);
    }

    /**
     *
     * <br>
     * Created 2014年9月11日 下午11:39:48
     *
     * @author wat.ztag
     */
    private void addListeners() {
        holder.btnRight.setOnClickListener(this);
        holder.btnLeft.setOnClickListener(this);
        holder.lst.setOnItemClickListener(mAdapter);
    }

    /**
     *
     * <br>
     * Created 2014年9月11日 下午11:39:53
     *
     * @author wat.ztag
     */
    private void changeLoopType() {
        mCurrentLoopType++;
        mCurrentLoopType %= mLoopTypeResource.length;
        holder.btnLeft.setImageResource(mLoopTypeResource[mCurrentLoopType]);
        holder.txtTitle.setText(getString(mTitleResource[mCurrentLoopType], singles.size()));
        ManagerFactory.getPlayerInstance().updateLoopType(this.getActivity(), mCurrentLoopType);
    }

    /**
     *
     * <br>
     * Created 2014年9月11日 下午11:39:57
     *
     * @version PlayListFragment
     * @author wat.ztag
     *
     * @see
     */
    private class GetMusicListOfPlaying extends NdTask {

        /**
         * Creates a new instance of GetMusicListOfPlaying. <br>
         * Created 2014年9月11日 下午11:40:03
         *
         * @param context Context
         * @param resId int
         */
        public GetMusicListOfPlaying(Context context, int resId) {
            super(context, resId);
        }

        @Override
        protected void doSuccess(Object result) {
            if (result != null) {
                singles = (List<MusicInfo>) result;
                mAdapter.setDatas(singles);
                holder.txtTitle
                        .setText(getString(mTitleResource[mCurrentLoopType], singles.size()));
            }
        }

        @Override
        protected void doFail(NdException result) {
            Toast.makeText(mContext, result.getSimpleMsg(), Toast.LENGTH_LONG).show();
        }

        @Override
        protected Object doInBackground(Object... arg0) {
            return ManagerFactory.getPlaylistInstance(getActivity()).getMusicListOfPlaying();
        }
    }

    /**
     *
     * <br>
     * Created 2014年9月11日 下午11:40:09
     *
     * @version PlayListFragment
     * @author wat.ztag
     *
     * @see
     */
    private class MusicReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(MusicBoxUtils.getMusicServiceAction())) {
                if (intent.hasExtra(MusicService.MUSIC_INFO)) {
                    mAdapter.setPlayId();
                }
            }
        }
    };

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btnLeft) {
            changeLoopType();

        } else if (id == R.id.btnRight) {
            getActivity().finish();

        }

    }
}
